import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class LoginPanel extends JPanel {

	private static final long serialVersionUID = -1551200197942157104L;


	//	// online server
	//	private static String default_server = "sql2.freesqldatabase.com:3306";
	//	private static String default_database = "sql29669";
	//	private static String default_username = "sql29669";
	//	private static String default_password = "fN9*vS6!";
	//

	//*/
	// local server
	private static String default_server = "localhost";
	private static String default_database = "moviestcd";
	private static String default_username = "root";
	private static String default_password = "1234";
	//*/


	private DatabaseInfo db_info;

	private JTextField txtDbServer;
	private JTextField txtDatabase;
	private JTextField txtUsername;
	//private JTextField txtPwd;
	private JPasswordField txtPwd;
	private JTextField textField_4;

	//private Db database;

	private UIFrame my_frame;


	private String adminFor;
	private boolean db_failed;

	/**
	 * Create the panel.
	 * @throws Exception 
	 */
	public LoginPanel(UIFrame the_frame) throws Exception {

		setLayout(null);
		db_failed = false;
		//database = Db.getDb();
		//database.connectToDB("sql29669", "sql2.freesqldatabase.com:3306", "sql29669", "fN9*vS6!");
		my_frame = the_frame;

		JLabel label = new JLabel("DB Server:");
		label.setBounds(48, 88, 69, 14);
		add(label);

		txtDbServer = new JTextField(default_server);
		txtDbServer.setColumns(10);
		txtDbServer.setBounds(113, 85, 135, 20);
		add(txtDbServer);

		JLabel label_1 = new JLabel("Database:");
		label_1.setBounds(48, 116, 71, 17);
		add(label_1);

		txtDatabase = new JTextField(default_database);
		txtDatabase.setColumns(10);
		txtDatabase.setBounds(113, 114, 135, 20);
		add(txtDatabase);

		JLabel label_2 = new JLabel("Username:");
		label_2.setBounds(48, 142, 69, 14);
		add(label_2);

		txtUsername = new JTextField(default_username);
		txtUsername.setColumns(10);
		txtUsername.setBounds(113, 139, 135, 20);
		add(txtUsername);

		JLabel label_3 = new JLabel("Password:");
		label_3.setBounds(48, 170, 69, 14);
		add(label_3);

		//txtPwd = new JTextField(default_password);
		txtPwd = new JPasswordField(default_password);
		txtPwd.setColumns(10);
		txtPwd.setBounds(113, 167, 135, 20);
		add(txtPwd);

		JLabel label_4 = new JLabel("AccountID:");
		label_4.setBounds(200, 209, 69, 14);
		add(label_4);

		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(264, 206, 126, 20);
		add(textField_4);


		JButton button = new JButton("Login");
		button.setBounds(301, 236, 89, 23);
		button.addActionListener(new LoginListener());
		add(button);

		JLabel lblConnectToDatabase = new JLabel("Connect to Database and Login");
		lblConnectToDatabase.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblConnectToDatabase.setBounds(82, 23, 261, 31);
		add(lblConnectToDatabase);

		textField_4.requestFocus();
	}

	private boolean isValidAccount(DatabaseInfo dbi) {
		Db db = Db.getDb();
		String query = "SELECT members.accountID FROM members WHERE members.accountID='"
				+ dbi.getAccountID() + "';";

		String query2 = "SELECT members.adminFor FROM members WHERE members.accountID='"
				+ dbi.getAccountID() + "';";

		String result = null;
		adminFor = null;

		try {
			result = db.executeQuery(dbi.server, dbi.database, query, 
					dbi.user_name, dbi.password);
			adminFor = db.executeQuery(dbi.server, dbi.database, query2, 
					dbi.user_name, dbi.password);
			db_failed = false;
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null,
					"Failed to connect to the database.",
					"Database Connection Error",
					JOptionPane.ERROR_MESSAGE);
			db_failed = true;

		}

		if(result != null) {
			String name = result.substring(result.indexOf('\t')).trim();
			if(0 < name.length() && name.equals(dbi.getAccountID()))
				return true;
		}
		return false;
	}

	private class LoginListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			db_info = new DatabaseInfo(txtDbServer.getText(), txtDatabase.getText(),
					txtUsername.getText(), new String(txtPwd.getPassword()), textField_4.getText());

			if(isValidAccount(db_info)) {
				my_frame.getContentPane().removeAll();
				JTabbedPane tabbedPane = new JTabbedPane();

				if (adminFor.contains("null")) {
					tabbedPane.addTab("Showings", new UserShowingsPanel(db_info));
					tabbedPane.addTab("Purchases", new UserPurchasesPanel(db_info));
				} else {
					tabbedPane.addTab("Showings", new UserShowingsPanel(db_info));
					tabbedPane.addTab("Purchases", new AdminPurchasesPanel(db_info));
					tabbedPane.addTab("My Cinema", new AdminEditShowingsPanel(db_info));
				}

				JLabel username = new JLabel("Logged in as: ");
				username.setFont(new Font("Tahoma", Font.PLAIN, 12));
				username.setBounds(350, 4, 100, 19);
				my_frame.getContentPane().add(username);

				JLabel id = new JLabel(db_info.getAccountID());
				id.setFont(new Font("Tahoma", Font.BOLD, 12));
				id.setBounds(427, 4, 150, 19);
				my_frame.getContentPane().add(id);


				my_frame.getContentPane().add(tabbedPane);
				my_frame.setPreferredSize(new Dimension(800, 430));
				my_frame.setMinimumSize(my_frame.getPreferredSize());
				my_frame.validate();
				my_frame.repaint();
				my_frame.pack();
			}
			else {
				if (!db_failed)
					JOptionPane.showMessageDialog(null,
							"Account ID not found.\n(Account IDs are case sensitive)",
							"Login Error",
							JOptionPane.ERROR_MESSAGE);
			}


		}
	}
}
